<template>
  <div class="common-layout">
    <el-container class="common-layout__container">
      <el-header class="common-layout__header fixed-header">
        <div class="header-brand">
          <img src="@/assets/assets/logo.png" alt="Logo" style="height: 30px; margin-right: 10px" />
          <span class="brand-text">战略微头条</span>
        </div>
        <div class="user-menu">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <el-avatar
                size="small"
                src="http://localhost:3000/uploads/avatar-1761015470147-810015669.svg"
              />
              <span style="margin-left: 8px">个人</span>
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-container class="content-container">
        <el-aside width="200px" class="common-layout__aside fixed-sidebar">
          <el-menu class="sidebar-menu" :default-active="activeMenu">
            <el-menu-item index="1">
              <router-link to="/journalism" class="sty">新闻池</router-link>
            </el-menu-item>
            <el-menu-item index="2">
              <router-link to="/issue" class="sty">新闻发布</router-link>
            </el-menu-item>
            <el-menu-item index="3">
              <router-link to="/alreadyIssue" class="sty">已发布新闻</router-link>
            </el-menu-item>
            <el-menu-item index="4">系统管理</el-menu-item>
            <el-menu-item index="5">日志管理</el-menu-item>
            <el-menu-item index="6">通知管理</el-menu-item>
            <el-menu-item index="7">
              <router-link to="/i18n" class="sty">国际化管理</router-link>
            </el-menu-item>
          </el-menu>
        </el-aside>

        <el-main class="common-layout__main scrollable-content">
          <div class="header-section" :class="headerClass">
            <span class="header-title">{{ pageTitle }}</span>
            <el-button
              v-if="route.path === '/issue'"
              type="primary"
              size="small"
              @click="handlePreview"
              class="preview-btn"
            >
              预览
            </el-button>
            
          </div>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { ref, watch, computed } from 'vue'
import { ElNotification } from 'element-plus'
import { useRouter } from 'vue-router'
import { useRoute } from 'vue-router'
import { ArrowDown } from '@element-plus/icons-vue'
const route = useRoute()
const router = useRouter()
const pageTitle = ref('新闻池')

const routeTitleMap = {
  '/journalism': '新闻池',
  '/issue': '新闻发布',
  '/alreadyIssue': '已发布新闻',
  '/i18n': '国际化管理',
}

const routeClassMap = {
  '/journalism': 'news-pool-header',
  '/issue': 'issue-header',
  '/alreadyIssue': 'already-issue-header',
  '/i18n': 'i18n-header',
}

const headerClass = computed(() => {
  return routeClassMap[route.path] || 'news-pool-header'
})

watch(
  () => route.path,
  (newPath) => {
    pageTitle.value = routeTitleMap[newPath] || '新闻池'
  },
  { immediate: true },
)

const activeMenu = computed(() => {
  const path = route.path
  if (path === '/journalism') return '1'
  if (path === '/issue') return '2'
  if (path === '/alreadyIssue') return '3'
  return '1'
})

const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('token')
    ElNotification({
      title: '成功',
      message: '退出登录成功',
      type: 'success',
    })
    router.push('/login')
  }
}

const handlePreview = () => {
  router.push('/preview')
}
</script>

<style scoped>
.common-layout {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.common-layout__container {
  height: 100%;
}

.fixed-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  color: #333;
  line-height: 60px;
  border-bottom: 1px solid #e8e8e8;
  background-color: white;
  padding: 0 20px;
  display: flex;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
}

.header-brand {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  color: #000;
}

.user-menu {
  margin-left: auto;
}

.el-dropdown-link {
  cursor: pointer;
  display: flex;
  align-items: center;
  color: #333;
}

.el-dropdown-link:hover {
  color: #1890ff;
}

.brand-text {
  margin-left: 10px;
}

.content-container {
  margin-top: 60px;
  height: calc(100vh - 60px);
}

.fixed-sidebar {
  position: fixed;
  top: 60px;
  left: 0;
  bottom: 0;
  z-index: 900;
  overflow-y: auto;
  background-color: white;
  border-right: 1px solid #e8e8e8;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.05);
}

.sidebar-menu {
  height: 100%;
  border-right: none;
  background-color: transparent;
}

.sidebar-menu .el-menu-item {
  height: 50px;
  line-height: 50px;
  font-size: 14px;
  color: #333;
  transition: all 0.2s ease;
}

.sidebar-menu .el-menu-item:hover,
.sidebar-menu .el-menu-item.is-active {
  background-color: #f0f9ff !important;
  color: #1890ff;
  border-right: 3px solid #1890ff;
}

.sty {
  color: inherit;
  text-decoration: none;
  display: block;
  width: 100%;
  padding: 0;
}

.scrollable-content {
  margin-left: 200px;
  margin-top: 0;
  height: 100%;
  overflow: auto;
  background-color: #f5f5f5;
  padding: 20px;
}

.header-section {
  padding: 20px;
  border-bottom: 1px solid #e8e8e8;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-title {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  color: #333;
}

.news-pool-header {
  background-color: #ffffff;
}

.news-pool-header .header-title {
  color: #333;
}

.issue-header {
  background-color: #f0f9ff;
}

.issue-header .header-title {
  color: #1890ff;
}

.already-issue-header {
  background-color: #f6ffed;
}

.already-issue-header .header-title {
  color: #52c41a;
}

.search-box {
  width: 300px;
  margin-bottom: 20px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 14px;
  outline: none;
}

.filter-label {
  font-size: 14px;
  color: #666;
  margin-right: 10px;
}

.filter-options {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.filter-options span {
  font-size: 14px;
  cursor: pointer;
  color: #666;
}

.filter-options span.active {
  color: #1890ff;
  font-weight: bold;
}

.news-item {
  background: white;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  padding: 16px;
  margin-bottom: 12px;
  transition: all 0.2s ease;
}

.news-item:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.news-category {
  display: inline-block;
  padding: 4px 8px;
  font-size: 12px;
  color: white;
  border-radius: 4px;
  margin-right: 10px;
  background-color: #f56a00;
}

.news-category.gray {
  background-color: #ccc;
  color: #666;
}

.news-category.green {
  background-color: #52c41a;
}

.news-title {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 8px 0;
  line-height: 1.5;
}

.news-summary {
  font-size: 14px;
  color: #666;
  margin: 10px 0;
  line-height: 1.6;
}

.news-meta {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  color: #999;
  margin-top: 10px;
}

.news-source {
  display: flex;
  align-items: center;
  gap: 8px;
}

.news-source img {
  width: 16px;
  height: 16px;
  border-radius: 50%;
}

.news-time {
  color: #999;
}

.news-actions {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.news-actions a {
  color: #1890ff;
  text-decoration: none;
  font-size: 14px;
}

.news-actions .edit {
  color: #1890ff;
}

.news-actions .pending {
  color: #1890ff;
}
</style>
